package com.feilong.edgeAI.remind.alarm;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class AlarmClockHelper {

    public static int INTERVAL_NONE = 0;
    public static int INTERVAL_HOUR = 1;
    public static int INTERVAL_DAY = 2;
    public static int INTERVAL_WEEK = 3;
    public static int INTERVAL_MONTH = 4;
    public static int INTERVAL_YEAR = 5;

    //一分钟
    public static int MINUTE = 60 * 1000;

    /**
     * 取消闹钟
     * @param context 上下文
     * @param alarmTime  取消闹钟的时间
     */
    public static void cancelAlarmClock(Context context, long alarmTime) {
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent alarmIntent = new Intent(context, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, (int) alarmTime / 1000, alarmIntent, PendingIntent.FLAG_IMMUTABLE);
        alarmManager.cancel(pendingIntent);
    }


    /**
     * 设置循环闹钟
     * @param context 上下文
     * @param alarmTime  提醒时间
     * @param interval 间隔时间  0单次提醒  1每小时  2每天 3每周 4每月 5每年 6自定义时间(单位分钟)
     */
    public static void setCirculateAlarmClock(Context context, long alarmTime, String interval) {
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        // 创建一个Intent，用于指定闹钟触发后要执行的行为
        Intent alarmIntent = new Intent(context, AlarmReceiver.class); // AlarmReceiver是 BroadcastReceiver 的子类
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, (int) alarmTime / 1000, alarmIntent, PendingIntent.FLAG_IMMUTABLE);

        long intervalTime = MINUTE;
        Log.i("AAAAA", "setCirculateAlarmClock: "+ Long.valueOf(interval));
        switch (interval) {
            case "0":
                alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
                break;
            case "1":
                intervalTime = MINUTE *60;
                break;
            case "2":
                intervalTime = MINUTE * 60 * 24 ;
                break;
            case "3":
                intervalTime = MINUTE * 60 * 24 * 7 ;
                break;
            case "4:":
                intervalTime = MINUTE * 60 * 24 * 30 ;
                break;
            case "5":
                intervalTime = MINUTE * 60 * 24 * 365 ;
                break;

            default:

                intervalTime = (long) MINUTE * Long.valueOf(interval);;

                break;

        }
        if(!interval.equals("0")){
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, intervalTime , pendingIntent);
        }


    }


}
